/*
 * @Author: hongbin
 * @Date: 2022-12-05 13:54:22
 * @LastEditors: hongbin
 * @LastEditTime: 2022-12-05 16:46:00
 * @Description:添加模型信息
 */
import type { NextApiRequest, NextApiResponse } from "next";
import { VerifyFields } from "../../../src/api/utils";
import DB from "../mongodb";
import { receiveRequest } from "../utils";

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
    const db = await DB;
    if (!receiveRequest(req, res, "POST")) return;
    // 通过socket发送申请加好友消息同意后双方均执行此请求
    console.log("请求内容", req.body);
    if (!VerifyFields.has(req.body, ["name", "fileName", "order"])) {
        return res.status(500).json("缺少关键字段");
    }

    const m = {
        name: req.body.name,
        fileName: req.body.fileName,
    };

    const insert = await db.collection("models").insertOne({ isUse: true, ...m });
    console.log("添加模型结果：", insert);
    if (insert.insertedId) {
        res.json({ data: insert });
    } else res.json({ data: null, msg: "失败" });
}
